package com.hualude.roadsign.controller;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.JsonMsgDto;
import com.hualude.roadsign.service.MachineLocationCountService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

/**
 * Created by pjn on 2017/9/20.
 */
@Controller
@RequestMapping("/rds_machineLocationCount")
public class MachineLocationCountController {
    @Resource
    private MachineLocationCountService machineLocationCountService;

    @RequestMapping(value = "/getMachine4DT")
    @ResponseBody
    public DTResponseDto getMachine4DT(Integer userId, Integer orgId, DTRequestDto dtRequestDto) {
        DTResponseDto dtResponseDto = new DTResponseDto();
        try {
            dtResponseDto = machineLocationCountService.getMachine4DT(userId, orgId, dtRequestDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dtResponseDto;
    }

    @RequestMapping(value = "/getProvinceName")
    @ResponseBody
    public JsonMsgDto getProvinceName(Integer userId, Integer orgId) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(machineLocationCountService.getProvinceName(userId, orgId));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping(value = "/getCityName")
    @ResponseBody
    public JsonMsgDto getCityName(Integer userId, Integer orgId, String provinceName) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(machineLocationCountService.getCityName(userId, orgId, provinceName));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping(value = "/getAllAreaName")
    @ResponseBody
    public JsonMsgDto getAllAreaName(Integer userId, Integer orgId, String provinceName, String cityName) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(machineLocationCountService.getAllAreaName(userId, orgId, provinceName, cityName));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonMsgDto;
    }


    @RequestMapping(value = "/getStartRoadName")
    @ResponseBody
    public JsonMsgDto getStartRoadName(Integer userId, Integer orgId, String provinceName, String cityName,
                                       String areaName, String endRoadName) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(machineLocationCountService.getStartRoadName(userId, orgId, provinceName, cityName, areaName,
                    endRoadName));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping(value = "/getEndRoadName")
    @ResponseBody
    public JsonMsgDto getEndRoadName(Integer userId, Integer orgId, String startRoad, String provinceName, String cityName,
                                     String areaName) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(machineLocationCountService.getEndRoadName(userId, orgId, startRoad, provinceName, cityName, areaName));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping(value = "/getSignalMachineTypeCounts")
    @ResponseBody
    public JsonMsgDto getSignalMachineTypeCounts(Integer userId, Integer orgId, String time, String province, String city, String area) {
        JsonMsgDto jsonMsgDto = new JsonMsgDto();
        try {
            jsonMsgDto.setJsonData(machineLocationCountService.getSignalMachineTypeCounts(userId, orgId, time, province, city, area));
            jsonMsgDto.setIsSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonMsgDto;
    }

    @RequestMapping(value = "/downMachineByExcel")
    @ResponseBody
    public void downMachineByExcel(HttpServletRequest request, HttpServletResponse response, String provinceName, String cityName, String areaName, Integer orgChoose, Integer userId, Integer orgId) {
        try {
            String search = provinceName + "#" + cityName + "#" + areaName + "#" + orgChoose;
            OutputStream ouputStream = response.getOutputStream();
            HSSFWorkbook result = machineLocationCountService.downMachineByExcel(search, userId, orgId);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            String userAgent = request.getHeader("User-Agent");
            userAgent = userAgent.toLowerCase();
            //判断浏览器版本
            String exportName = "信号机统计报表";
            if (userAgent.indexOf("msie") != -1) {
                response.setHeader("Content-disposition", "attachment;filename=" + new String(exportName.getBytes("gb2312"), "ISO8859-1") + ".xls");
            } else {
                response.setHeader("Content-disposition", "attachment;filename=" + new String(exportName.getBytes("UTF-8"), "ISO8859-1") + ".xls");
            }
            result.write(ouputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
